package com.openbravo.pos.panels;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.openbravo.bean.ref.I_RefCustomer;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.pos.base.AppLocal;
import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

public abstract class AbstractRefCustomerEditor<T extends I_RefCustomer> extends
		AbstractChildDAOJEditor<T, String> {

	private final I_DataLogicSales m_dlSales;
	private JTextField jtxtCustomer;
	private I_DataLogicCustomers dlCustomers;
	private JButton btnCustomer;


    public AbstractRefCustomerEditor(I_DataLogicSales app,I_DataLogicCustomers dlCustomers, DirtyManager dirty) {
        super(dirty);
        m_dlSales = app;
        this.dlCustomers=dlCustomers;
    	initComponents();
    }
    private void initComponents() {
    	setPreferredSize(new Dimension(450, 43));
    	
    	JLabel labelCustomer = new JLabel();
    	labelCustomer.setText(AppLocal.getIntString("label.customer"));
    	add(labelCustomer);
    	
    	jtxtCustomer = new JTextField();
    	jtxtCustomer.setEditable(false);
    	jtxtCustomer.setPreferredSize(new Dimension(200, 25));
    	add(jtxtCustomer);
    	
    	btnCustomer = new JButton();
    	btnCustomer.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			btnCustomerActionPerformed(e);
    		}
    	});
    	btnCustomer.setRequestFocusEnabled(false);
    	btnCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/kuser.png"))); // NOI18N
    	btnCustomer.setPreferredSize(new Dimension(50, 25));
    	btnCustomer.setMargin(new Insets(8, 14, 8, 14));
    	btnCustomer.setFocusable(false);
    	btnCustomer.setFocusPainted(false);
    	add(btnCustomer);
    }


    private void btnCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomerActionPerformed
        JCustomerFinder finder = JCustomerFinder.getCustomerFinder(this, m_dlSales);
        finder.search(obj.getCustomer());
        finder.setVisible(true);
        obj.setCustomer(finder.getSelectedCustomer());
        obj.setCustomerId(finder.getSelectedCustomer() == null
                    ? null
                    : finder.getSelectedCustomer().getId());
        
        jtxtCustomer.setText(finder.getSelectedCustomer() == null
                    ? null
                    : finder.getSelectedCustomer().getName());
        dirty.setDirty(true);
    }
    

	@Override
	public void refresh() {
		
	}


	@Override
	public void updateUI(boolean editAble, int state) {
		btnCustomer.setEnabled(editAble);
		jtxtCustomer.setText(obj.getCustomer() == null
                ? null
                : obj.getCustomer().getName());
	}

}
